package fenyu.personblob.web.service;

import fenyu.personblob.bean.databean.User;
import fenyu.personblob.bean.databean.blob.Blob;
import fenyu.personblob.bean.databean.user.CustomUser;
import fenyu.personblob.bean.databean.user.DefaultUser;
import fenyu.personblob.bean.strategy.Strategy;
import fenyu.personblob.single.SinglePathDoor;
import fenyu.personblob.web.model.ApiReponse;
import fenyu.personblob.web.model.BlobShow;
import fenyu.personblob.web.model.BlobShowSimple;
import fenyu.personblob.web.util.TokenGetUserStrategy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * create at 2018/12/22 12:23
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
@Service
public class BlobService {

    private Strategy tokenGetUserStrategy = new TokenGetUserStrategy();

    public ApiReponse<List<BlobShowSimple>> bloblist() {
        List<Blob> blobs = SinglePathDoor.getSingleSave().getSaveDoor().getBlobSavePath().getAll();
        List<BlobShowSimple> blobShowSimples = new ArrayList<>();
        for (Blob blob : blobs) {
            blobShowSimples.add(new BlobShowSimple(blob));
        }
        return new ApiReponse<>(blobShowSimples);
    }

    public ApiReponse<BlobShow> singleblob(Integer blob_id) {
        Blob blob = SinglePathDoor.getSingleSave().getSaveDoor().getBlobSavePath().getById(blob_id);
        if (blob != null)
            return new ApiReponse<>(new BlobShow(blob));
        else return new ApiReponse<>(400, "没有目标");
    }

    public ApiReponse<String> sendBlob(String token, String tittle, String text) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (!(user instanceof DefaultUser))
            return new ApiReponse<>(503, "no power");
        ((DefaultUser) user).sendBlobStrategy(tittle, text);
        return new ApiReponse<>("ok");
    }


    public ApiReponse<String> commentBlob(String token, String text, Integer blob_id, Integer comment_id) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (!(user instanceof CustomUser))
            return new ApiReponse<>(503, "no power");
        if (comment_id != null)
            ((CustomUser) user).comment(text, blob_id, comment_id);
        else ((CustomUser) user).comment(text, blob_id);
        return new ApiReponse<>("ok");
    }

}
